import hashlib


class dataholder():
    rawData=""
    def __init__(self,phrase=""):
        if (phrase!=""): self.CreateRawData(phrase)
        self.index=0

    def CreateRawData(self,phrase=""):
        """Create Raw Data from sPhrase by using sha512"""
        if phrase!="": self.phrase=phrase
        self.rawData=hashlib.sha512(self.phrase).hexdigest()

    def __inc__(self):
        self.index+=1
        if (self.index>=len(self.rawData)):
            self.index=0

    def getc(self):
        ret=self.rawData[self.index]
        self.__inc__()
        return ret

    def getnrstr(self,nr):
        ret=""
        if nr>0:
            for a in range(nr):
                ret+=self.getc()
        return ret
                

    def ibyte0to15(self):
        """Return a integer 0-15 (one char)"""
        return int(self.getc(),16)

    def ibytes(self,n):
        """Return a int specified by bytes"""
        if n>0:
            return int(self.getnrstr(n),16)
        else: return 0

    def fbytes(self,n,m):
        """Return a float specifid by bytes"""
        return float(str(self.ibytes(n))+"."+str(self.ibytes(m)))

    def ik(self):
        """ 0 - 1K """
        return int(float(self.ibytes(6))/float(int("ffffff",16))*1000)

    def im(self):
        """ 0 - 1 Meg """
        return int(float(self.ibytes(6))/ float(int("ffffff",16))*1000000)

    def get0to(self,x):
        """ get a number from dh that is 0 to x, x should be less then 100 """
        x+=1
        ik=self.ik()
        return int((ik/1000.0)*x)

    def tof(self, chans=50):
        """ returns true or false, the chans variable is the chans for True and is default 50% """
        if((self.ibytes(2)/255.0)*100.0) <= chans : return True
        else: return False



if __name__ == "__main__":
    data=dataholder()
    data.CreateRawData("9kdsdloo0001")    
    print "RawData:  "+data.rawData
    print "One byte: "+str(data.ibyte0to15())
    print "7 chars: "+data.getnrstr(7)
    print "3 chars: " + data.getnrstr(3)
    print "1 char:"+data.getnrstr(1)
    print "One byte: "+str(data.ibyte0to15())
    print "From 2 Bytes :"+str(data.ibytes(2))
    print "From 3 Bytes :"+str(data.ibytes(3))
    print "From 4 Bytes :"+str(data.ibytes(4))
    print "From 5 Bytes :"+str(data.ibytes(5))
    print "From 6 Bytes :"+str(data.ibytes(6))
    print "97 chars: "+data.getnrstr(97)
    print "float from 3,5 char: "+str(data.fbytes(3,5))
    print "float from 0,5 char: "+str(data.fbytes(0,5))
    print "0-1000: "+str(data.ik())
    print "0-1000000 :"+str(data.im())
    print "0-2:%s",data.get0to(2)
    for i in range(100):
        print "0-2:%s",data.get0to(2)
